

global.util = {
    //安全取值
    getValue:function(fn ,deafultValue){
        try{
            let result = fn();
            if(result != undefined){
                return result;
            }else{
                return deafultValue;
            }
        }catch(error){
            return deafultValue;
        }
    },
    //判断有无该值
    hasKey:function(fn){
        try{
            let result = fn();
            if(result != undefined){
                return true;
            }else{
                return false;
            }
        }catch(e){
            return false;
        }
    },
    //判断是否是数组
    isArray:function(obj){
        if(typeof obj == "object" && obj.constructor == Array){
            return true;
        }
     return false;
    },
    //结构检查
    checkStructure:function(srcData,data){
        for(key in data){
            let node = data[key];
            let srcNode = srcData[key];

            //检查有无对应的key
            if(srcNode == undefined){
                return false;
            }

            //判断node是不是一个对象或数组
            if((typeof node) == 'object'){
                //迭代检查结构
                if(!util.checkStructure(srcNode,node)){
                    return false;
                }
                continue;
            }

            //判断类型是否正确
            if((typeof srcNode) != node){
                return false;
            }
        }
        return true;
    },
    checkStructure_throw:function(srcData,data){
        if(!util.checkStructure(srcData,data)){
            throw TypeError("[10144]对象结构或数据类型不正确\n"
                + "传入的数据：\n"
                + JSON.stringify(srcData) +"\n"
                + "这些数据被要求至少包含下列字段和字段类型：\n"
                + JSON.stringify(data)
                + "\n");
        }
    }
}


//链表
global.util.LinkedQueue = function(){

    let node = function(element){
        this.element = element;
        this.next = null;
        this.before = null;
    }

    this.head = null; //头节点
    this.foot = null; //尾节点
    this.length = 0;


    this.pushHead = function(element){
        //创建一个节点
        let nd = new node(element);

        //连接到链表头部
        if(this.head == null){
            //如果链表为空
            //将这个节点设置为头节点和尾节点
            this.head = nd;
            this.foot = nd;
        }else{
            //结点连接
            nd.before = this.haed;
            this.head.next = nd;

            //设置头
            this.head = nd;
        }

        this.length++;

    }

    this.pushFoot = function(element){
        let nd = new node(element);

        if(this.foot == null){
            this.foot = nd;
            this.head = nd;
        }else{
            nd.next = this.foot;
            this.foot.before = nd;

            this.foot = nd;
        }

        this.length++;
    }

    this.popHead = function(){
        if(this.head == null){
            return null;
        }else{
            let node = this.head;
            let beforeNode = node.before;
            if(beforeNode == null){//只有一个节点的情况
                this.head = null;
                this.foot = null;
                this.length--;
                return node.element;
            }else{
                beforeNode.next = null;
                this.head = beforeNode;
                this.length--;
                return node.element;
            }
        }
    }

    this.popFoot = function(){
        if(this.foot == null){
            return null;
        }else{
            let node = this.foot;
            let nextNode = node.next;
            if(nextNode == null){
                this.head = null;
                this.foot = null;
                this.length--;
                return node.element;
            }else{
                nextNode.before = null;
                this.foot = nextNode;
                this.length--;
                return node.element;
            }
        }
    }

    this.display = function(){
        //从尾部遍历
        if(this.foot != null){
            let nd = this.foot;
            console.log(JSON.stringify(nd.element));
            while(nd.next != null){
                nd = nd.next;
                console.log(JSON.stringify(nd.element));
            }
        }else{
            console.log();
        }

        console.log("Length:"+this.length);
    }

    this.clean = function(){
        this.head = null; //头节点
        this.foot = null; //尾节点
        this.length = 0;
    }

    this.serialize = function(){
        /**
         * 序列化步骤
         * 1.将所有内容从尾部到头部取出数据并JSON序列化添加到数组
         * 2.将数组序列化
         */
        if(this.foot != null){
            let data = [];
            let nd = this.foot;
            data.push(JSON.stringify(nd.element));
            while(nd.next != null){
                nd = nd.next;
                data.push(JSON.stringify(nd.element));
            }
            return JSON.stringify(data);
        }else{
            return JSON.stringify([]);
        }
    }

    this.deserialize = function(data){
        data = JSON.parse(data);
        this.clean();
        for(let item of data){
            this.pushHead(JSON.parse(item));
        }
    }

}
